package com.meilai.project.controller.web.business.installOrder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.installOrder.WorkerSubmitCheckDTO;
import com.meilai.project.dto.business.installOrder.WorkerSubmitListDTO;
import com.meilai.project.service.business.installOrder.WorkerSubmitPhotoService;
import com.meilai.project.service.business.installOrder.WorkerSubmitService;
import com.meilai.project.vo.business.installOrder.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * @author xchen
 * @since 2022-03-26
 */
@Api(value = "工单审核", tags = {"工单审核"})
@RestController
@RequestMapping("api/web/work_submit")
public class WorkerSubmitController {

    @Autowired
    private WorkerSubmitService workerSubmitService;

    @Autowired
    private WorkerSubmitPhotoService workerSubmitPhotoService;

    @GetMapping("status_count")
    @ApiOperation("获取各种单据各状态对应数量")
    public ApiResult<List<WorkerSubmitStatusCountVO>> workerSubmitStatusCount(
            @ApiParam(value = "workOrderId") @RequestParam("workOrderId") Long workOrderId
    ) {
        List<WorkerSubmitStatusCountVO> result = workerSubmitService.workerSubmitStatusCount(workOrderId);
        return ApiResult.ok(result);
    }

    @PostMapping("check_list")
    @ApiOperation("审核列表")
    public ApiResult<Page<WorkerSubmitVO>> workerSubmitCheckList(
            @ApiParam("json") @RequestBody WorkerSubmitListDTO workerSubmitStatus
    ) {
        Page<WorkerSubmitVO> workerSubmitVOS = workerSubmitService.workerSubmitCheckList(workerSubmitStatus);
        return ApiResult.ok(workerSubmitVOS);
    }

    @GetMapping("worker_order_list")
    @ApiOperation("获取工单下拉选择项")
    public ApiResult<List<WorkerOrderSelectOptionsVO>> workerOrderSelectOptions(
            @ApiParam(value = "workerSubmitId") @RequestParam("workerSubmitId") Long workerSubmitId
    ) {
        List<WorkerOrderSelectOptionsVO> result = workerSubmitService.workerOrderSelectOptions(workerSubmitId);
        return ApiResult.ok(result);
    }

    @GetMapping("new_install_building_list_and_media_type")
    @ApiOperation("新安装审核获取楼盘列表&媒体类型")
    public ApiResult<List<NewInstallBuildingAndMediaTypeVO>> newInstallBuildingAndMediaTypeVO(
            @ApiParam(value = "workOrderId") @RequestParam("workOrderId") Long workOrderId) {
        List<NewInstallBuildingAndMediaTypeVO> result = workerSubmitService.newInstallBuildingAndMediaTypeVO(workOrderId);
        return ApiResult.ok(result);
    }

    @PostMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(
            @ApiParam(value = "json") @RequestBody WorkerSubmitCheckDTO workerSubmitCheckDTO) {
        workerSubmitService.pass(workerSubmitCheckDTO);
        return ApiResult.ok();
    }

    @PostMapping("no_pass")
    @ApiOperation("不通过")
    public ApiResult<Boolean> noPass(
            @ApiParam(value = "json") @RequestBody List<Long> workerSubmitIdList) {
        workerSubmitService.changeCheckStatus(2, workerSubmitIdList);
        return ApiResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("删除接口")
    public ApiResult<Boolean> delete(@ApiParam(value = "json") @RequestBody List<Long> workerSubmitIdList) {
        workerSubmitService.delete(workerSubmitIdList);
        return ApiResult.ok();
    }

    @ApiOperation(value = "安装统计", notes = "安装统计", response = WorkerSubmitVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/page")
    public CommonResult page(@ApiParam(name = "page", value = "page", required = true)
                             @RequestParam(value = "page") Integer page,
                             @ApiParam(name = "page_size", value = "page_size", required = true)
                             @RequestParam(value = "page_size") Integer page_size,
                             @ApiParam(name = "begin_time", value = "开始时间")
                             @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                             @RequestParam(value = "begin_time", required = false) LocalDate begin_time,
                             @ApiParam(name = "end_time", value = "结束时间")
                             @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                             @RequestParam(value = "end_time", required = false) LocalDate end_time,
                             @ApiParam(name = "begin_time_save", value = "开始时间")
                                 @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                 @RequestParam(value = "begin_time_save", required = false) LocalDate begin_time_save,
                             @ApiParam(name = "end_time_save", value = "结束时间")
                                 @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                 @RequestParam(value = "end_time_save", required = false) LocalDate end_time_save,
                             @ApiParam(name = "work_order_id", value = "工单ID")
                             @RequestParam(value = "work_order_id", required = false) Long work_order_id,
                             @ApiParam(name = "worker_id", value = "工人ID")
                             @RequestParam(value = "worker_id", required = false) Long worker_id,
                             @ApiParam(name = "work_order_type_ids", value = "工单类型ID集合")
                             @RequestParam(value = "work_order_type_ids", required = false) List<Integer> work_order_type_ids,
                             @ApiParam(name = "status", value = "状态")
                             @RequestParam(value = "status", required = false) Integer status,
                             @ApiParam(name = "code", value = "编号")
                             @RequestParam(value = "code", required = false) String code,
                             @ApiParam(name = "building_area_id", value = "项目ID")
                             @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                             @ApiParam(name = "media_type_id", value = "媒体类型ID")
                             @RequestParam(value = "media_type_id", required = false) Long media_type_id,
                             @ApiParam(name = "statistics", value = "是否是统计工作量")
                             @RequestParam(value = "statistics", required = false) Boolean statistics) {
        return new CommonResult().success(workerSubmitService.page(page, page_size, begin_time, end_time, work_order_id, worker_id, work_order_type_ids, status, code, building_area_id, media_type_id, statistics, begin_time_save, end_time_save));
    }

    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/exportExcel")
    public void exportExcel(@ApiParam(name = "page", value = "page", required = true)
                            @RequestParam(value = "page") Integer page,
                            @ApiParam(name = "page_size", value = "page_size", required = true)
                            @RequestParam(value = "page_size") Integer page_size,
                            @ApiParam(name = "begin_time", value = "开始时间")
                            @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                            @RequestParam(value = "begin_time", required = false) LocalDate begin_time,
                            @ApiParam(name = "end_time", value = "结束时间")
                            @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                            @RequestParam(value = "end_time", required = false) LocalDate end_time,
                            @ApiParam(name = "begin_time_save", value = "开始时间")
                                @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                @RequestParam(value = "begin_time_save", required = false) LocalDate begin_time_save,
                            @ApiParam(name = "end_time_save", value = "结束时间")
                                @JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
                                @RequestParam(value = "end_time_save", required = false) LocalDate end_time_save,
                            @ApiParam(name = "work_order_id", value = "工单ID")
                            @RequestParam(value = "work_order_id", required = false) Long work_order_id,
                            @ApiParam(name = "worker_id", value = "工人ID")
                            @RequestParam(value = "worker_id", required = false) Long worker_id,
                            @ApiParam(name = "work_order_type_ids", value = "工单类型ID集合")
                            @RequestParam(value = "work_order_type_ids", required = false) List<Integer> work_order_type_ids,
                            @ApiParam(name = "status", value = "状态")
                            @RequestParam(value = "status", required = false) Integer status,
                            @ApiParam(name = "code", value = "编号")
                            @RequestParam(value = "code", required = false) String code,
                            @ApiParam(name = "building_area_id", value = "项目ID")
                            @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                            @ApiParam(name = "media_type_id", value = "媒体类型ID")
                            @RequestParam(value = "media_type_id", required = false) Long media_type_id,
                            HttpServletResponse response,
                            @ApiParam(name = "statistics", value = "是否是统计工作量")
                            @RequestParam(value = "statistics", required = false) Boolean statistics) {
        workerSubmitService.exportExcel(page, page_size, begin_time, end_time, work_order_id, worker_id, work_order_type_ids, status, code, building_area_id, media_type_id, response, statistics, begin_time_save, end_time_save);
    }


    @ApiOperation(value = "安装数量统计", notes = "安装数量统计", response = WorkerSubmitCountVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/installCountPage")
    public CommonResult installCountPage(@ApiParam(name = "page", value = "page", required = true)
                                         @RequestParam(value = "page") Integer page,
                                         @ApiParam(name = "page_size", value = "page_size", required = true)
                                         @RequestParam(value = "page_size") Integer page_size,
                                         @ApiParam(name = "startDate", value = "开始时间")
                                         @RequestParam(value = "startDate", required = false)
                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                         @ApiParam(name = "endDate", value = "结束时间")
                                         @RequestParam(value = "endDate", required = false)
                                         @JsonFormat(pattern = "yyyy-MM-dd")
                                         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
                                         @ApiParam(name = "worker_id", value = "工人ID")
                                         @RequestParam(value = "worker_id", required = false) Long worker_id,
                                         @ApiParam(name = "media_type_id", value = "媒体类型ID")
                                         @RequestParam(value = "media_type_id", required = false) Long media_type_id,
                                         @ApiParam(name = "work_order_type_id", value = "工单类型ID")
                                         @RequestParam(value = "work_order_type_id", required = false) Long work_order_type_id) {
        return new CommonResult().success(workerSubmitService.installCountPage(page, page_size, startDate, endDate, worker_id, media_type_id, work_order_type_id));
    }


    @ApiOperation(value = "安装数量统计", notes = "安装数量统计", response = WorkerSubmitCountVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("/installCountRecord")
    public CommonResult installCountRecord(@ApiParam(name = "page", value = "page", required = true)
                                           @RequestParam(value = "page") Integer page,
                                           @ApiParam(name = "page_size", value = "page_size", required = true)
                                           @RequestParam(value = "page_size") Integer page_size,
                                           @ApiParam(name = "startDate", value = "开始时间")
                                           @RequestParam(value = "startDate", required = false)
                                           @JsonFormat(pattern = "yyyy-MM-dd")
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                           @ApiParam(name = "endDate", value = "结束时间")
                                           @RequestParam(value = "endDate", required = false)
                                           @JsonFormat(pattern = "yyyy-MM-dd")
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
                                           @ApiParam(name = "worker_id", value = "工人ID")
                                           @RequestParam(value = "worker_id", required = false) Long worker_id,
                                           @ApiParam(name = "building_area_id", value = "楼盘ID")
                                           @RequestParam(value = "building_area_id", required = false) Long building_area_id,
                                           @ApiParam(name = "work_order_type_id", value = "工单类型ID")
                                           @RequestParam(value = "work_order_type_id", required = false) Long work_order_type_id,
                                           @ApiParam(name = "customer_id", value = "客户ID")
                                           @RequestParam(value = "customer_id", required = false) Long customer_id) {
        return new CommonResult().success(workerSubmitService.installCountRecord(page, page_size, startDate, endDate, worker_id, building_area_id, work_order_type_id, customer_id));
    }

    @PostMapping("/replacePhoto")
    @ApiOperation(value = "替换图片", notes = "替换图片", response = Boolean.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    public CommonResult replacePhoto(@RequestParam("id") Long id, @RequestParam("oldUrl") String oldUrl, @RequestParam("newUrl") String newUrl) {
        return new CommonResult().success(workerSubmitService.replacePhoto(id, oldUrl, newUrl));
    }

    @DeleteMapping("/removePhoto")
    @ApiOperation(value = "删除图片", notes = "删除图片", response = Boolean.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    public CommonResult removePhoto(@RequestParam("id") Long id) {
        return new CommonResult().success(workerSubmitPhotoService.removeById(id));
    }
}
